let app = getApp();
let httptool=require("../../../utils/zry.httptool.js");
Page({
  data:{
      deviceList:[],//设备列表
      msgprop:'',//消息提示 
      deviceName:'',
      noData: false,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    let that=this;
    that.getDeviceList();
  },
  getDeviceList:function(e){
    let that=this;
    httptool.ajaxPost("api/Device/GetMyConnectDevices", {},function(res){//设备列表
      if(res.suc){
          let data=app.notNull(res.data, []);
          for(let i=0;i<data.length;i++){
            data[i].operState='修改名称';
            data[i].isEdit=false;
            data[i].focus=false;
          }
          that.setData({ deviceList: data, noData: data.length == 0});
      }else{
        app.showNotify(that, res.msg);
      }
    });

  },
  bindChange:function(e){
    let that=this;
    that.setData({deviceName:e.detail.value});
    
  },
 
  modifyName:function(e){//修改设备名称
    let that=this;
    let index = e.currentTarget.dataset.index;
    if (that.data.deviceList[index].isEdit == false) {
      for (let i in that.data.deviceList){
        if(index==i){
          that.data.deviceList[i].isEdit = true;
          that.data.deviceList[i].operState = "完成";
          that.data.deviceList[i].focus = true;
          that.setData({ deviceName: that.data.deviceList[i].OtherName });
        }
        else{
          that.data.deviceList[i].isEdit = false;
          that.data.deviceList[i].operState = "修改名称";
          that.data.deviceList[i].focus = false;
        }
      }
      that.setData({ deviceList: that.data.deviceList });
    }
    else{
      let params = {}; 
      params.OtherName = that.data.deviceName;
      params.DeviceId = e.currentTarget.dataset.id;
      httptool.ajaxPost("api/Device/UpdateDeviceName", params, function (res) {//设备列表
        if (res.suc) {
          app.showSuccess({ msg:"修改成功!"});
          that.getDeviceList();
        } else {
          app.showNotify(that, res.msg);
        }
      });
    }    
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})